<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
#use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (Schema::hasTable('user_tokens')) return;
        Schema::create('user_tokens', function (Blueprint $table) {
            $table->id();
            $table->integer('user_id')->default(0)->comment('用户id');
            $table->char('token', 32)->default('')->comment('token');
            $table->boolean('type')->default(1)->comment('客户端类型：1小程序，2APP');
            $table->boolean('user_type')->default(1)->comment('用户类型：1租户，2顾问，3业主，4管理员');
            $table->integer('expire_time')->default(0)->comment('token过期时间戳');
            $table->dateTime('created_at')->default(\DB::raw('CURRENT_TIMESTAMP'));
            $table->timestamp('updated_at')->default(\DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
            $table->index('created_at');
            $table->unique(['user_id', 'type']);
            $table->unique('token');
        });
        DB::statement("ALTER TABLE `user_tokens` COMMENT '用户登录唯一标识表'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        if (!is_production()) Schema::dropIfExists('user_tokens');
    }
};
